package moe.bogos.douro.api.common.soul;

import moe.bogos.douro.api.common.Constants;
import moe.bogos.douro.api.common.DARegisties;
import moe.bogos.douro.api.common.capabilities.CapHelper;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.StringNBT;
import net.minecraft.util.ResourceLocation;

public class SoulHelper
{
    public Soul getPlayerSoul(PlayerEntity player)
    {
        String soulID = getPlayerSoulID(player);
        if (soulID == null) return null;
        ResourceLocation key = new ResourceLocation(soulID);
        return DARegisties.SOUL.getValue(key);
    }
    
    public String getPlayerSoulID(PlayerEntity player)
    {
        INBT nbt = CapHelper.get(player, Constants.SOUL);
        if (nbt == null || !(nbt instanceof StringNBT)) return null;
        return nbt.getAsString();
    }
    
    public void setPlayerSoul(Soul soul, PlayerEntity player)
    {
        setPlayerSoul(soul.getRegistryName().toString(), player);
    }
    
    public void setPlayerSoul(String soulID, PlayerEntity player)
    {
        if (DARegisties.SOUL.containsKey(new ResourceLocation(soulID)))
            CapHelper.set(player, Constants.SOUL, StringNBT.valueOf(soulID));
    }
}
